<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>Funktionen &mdash; PowerShell fuer Systemintegratoren</title>
    
    <link rel="stylesheet" href="../_static/sphinxdoc.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '1.0',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/translations.js"></script>
    <link rel="top" title="PowerShell fuer Systemintegratoren" href="../index.html" /> 
  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="Allgemeiner Index"
             accesskey="I">Index</a></li>
        <li><a href="../index.html">PS4SYS</a> &raquo;</li> 
      </ul>
    </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
            <p class="logo"><a href="../index.html">
              <img class="logo" src="../_static/PowerShell_icon_small.png" alt="Logo"/>
            </a></p>
  <h3>Diese Seite</h3>
  <ul class="this-page-menu">
    <li><a href="../_sources/loesung_rst/Loesung_PowerShell_Funktionen.txt"
           rel="nofollow">Quelltext anzeigen</a></li>
  </ul>
<div id="searchbox" style="display: none">
  <h3>Schnellsuche</h3>
    <form class="search" action="../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Los" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Geben Sie Suchbegriffe oder einen Modul-, Klassen- oder Funktionsnamen ein.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="funktionen">
<h1>Funktionen<a class="headerlink" href="#funktionen" title="Permalink zu dieser Überschrift">¶</a></h1>
<ul id="loesung-dir">
<li><p class="first">Schreiben Sie eine Funktion, die als Parameter einen Pfad erwartet und anschließend den Inhalt des Pfades ausgibt</p>
<blockquote>
<div><div class="highlight-sh"><div class="highlight"><pre><span class="k">function </span>listDirectory<span class="o">(</span><span class="nv">$pfad</span><span class="o">)</span>
<span class="o">{</span>

<span class="nb">cd</span> <span class="nv">$pfad</span>
Dir <span class="nv">$pfad</span> |Out-File  C:<span class="se">\t</span>emp<span class="se">\A</span>usgabe.txt

<span class="o">}</span>
</pre></div>
</div>
</div></blockquote>
</li>
</ul>
<ul id="loesung-dir-rekursiv">
<li><p class="first">Erweitern Sie die obige Funktion um eine rekursive Variante. Sollte der Pfad Unterordner besitzen, sollen auch diese ausgegeben werden.</p>
<blockquote>
<div><div class="highlight-sh"><div class="highlight"><pre><span class="k">function </span>listDirectory<span class="o">(</span><span class="nv">$pfad</span><span class="o">)</span>
<span class="o">{</span>

<span class="nb">cd</span> <span class="nv">$pfad</span>
Dir <span class="nv">$pfad</span> -recurse |Out-File  C:<span class="se">\t</span>emp<span class="se">\A</span>usgabe.txt

<span class="o">}</span>
</pre></div>
</div>
</div></blockquote>
</li>
</ul>
<ul id="loesung-dir-rekursiv-extension">
<li><p class="first">Erweitern Sie die obige Funktion: Es soll jetzt möglich sein, nach bestimmten Dateikennungen die Ausgabe zu gestalten</p>
<blockquote>
<div><div class="highlight-sh"><div class="highlight"><pre><span class="k">function </span>listDirectory<span class="o">(</span><span class="nv">$pfad</span>, <span class="nv">$ext</span><span class="o">)</span>
<span class="o">{</span>
        <span class="nb">cd</span> <span class="nv">$pfad</span>
        Dir <span class="nv">$pfad</span> -Recurse  -include *.<span class="nv">$ext</span> |Out-File  C:<span class="se">\t</span>emp<span class="se">\A</span>usgabe.txt
<span class="o">}</span>
</pre></div>
</div>
</div></blockquote>
</li>
<li><p class="first">Wie überprüfen Sie, ob ein Array einen bestimmten Wert enthält</p>
<blockquote>
<div><p>Folgende Funktion überprüft zwei Arrays auf Gleichheit</p>
<div class="highlight-sh"><div class="highlight"><pre><span class="k">function </span>AreArraysEqual<span class="o">(</span><span class="nv">$a1</span>, <span class="nv">$a2</span><span class="o">)</span> <span class="o">{</span>
        <span class="k">if</span> <span class="o">(</span><span class="nv">$a1</span> -isnot <span class="o">[</span>array<span class="o">]</span> -or <span class="nv">$a2</span> -isnot <span class="o">[</span>array<span class="o">])</span> <span class="o">{</span>
          throw <span class="s2">&quot;Both inputs must be an array&quot;</span>
        <span class="o">}</span>
        <span class="k">if</span> <span class="o">(</span><span class="nv">$a1</span>.Rank -ne <span class="nv">$a2</span>.Rank<span class="o">)</span> <span class="o">{</span>
          <span class="k">return</span> <span class="nv">$false</span>
        <span class="o">}</span>
        <span class="k">if</span> <span class="o">([</span>System.Object<span class="o">]</span>::ReferenceEquals<span class="o">(</span><span class="nv">$a1</span>, <span class="nv">$a2</span><span class="o">))</span> <span class="o">{</span>
          <span class="k">return</span> <span class="nv">$true</span>
        <span class="o">}</span>
        <span class="k">for</span> <span class="o">(</span><span class="nv">$r</span> <span class="o">=</span> 0; <span class="nv">$r</span> -lt <span class="nv">$a1</span>.Rank; <span class="nv">$r</span>++<span class="o">)</span> <span class="o">{</span>
          <span class="k">if</span> <span class="o">(</span><span class="nv">$a1</span>.GetLength<span class="o">(</span><span class="nv">$r</span><span class="o">)</span> -ne <span class="nv">$a2</span>.GetLength<span class="o">(</span><span class="nv">$r</span><span class="o">))</span> <span class="o">{</span>
                        <span class="k">return</span> <span class="nv">$false</span>
          <span class="o">}</span>
        <span class="o">}</span>

        <span class="nv">$enum1</span> <span class="o">=</span> <span class="nv">$a1</span>.GetEnumerator<span class="o">()</span>
        <span class="nv">$enum2</span> <span class="o">=</span> <span class="nv">$a2</span>.GetEnumerator<span class="o">()</span>

        <span class="k">while</span> <span class="o">(</span><span class="nv">$enum1</span>.MoveNext<span class="o">()</span> -and <span class="nv">$enum2</span>.MoveNext<span class="o">())</span> <span class="o">{</span>
          <span class="k">if</span> <span class="o">(</span><span class="nv">$enum1</span>.Current -ne <span class="nv">$enum2</span>.Current<span class="o">)</span> <span class="o">{</span>
                        <span class="k">return</span> <span class="nv">$false</span>
          <span class="o">}</span>
        <span class="o">}</span>
        <span class="k">return</span> <span class="nv">$true</span>
<span class="o">}</span>
</pre></div>
</div>
</div></blockquote>
</li>
<li><p class="first">Folgendes Powershell-Skript berechnet den Wochentag mit Hilfe eines eigenen Algorythmus. Verbessern Sie die Lesbarkeit des Skriptes, indem Sie die verschiedenen Teile des Quellcode mit Hilfe von Funktionen kapseln.</p>
<blockquote>
<div><div class="highlight-sh"><div class="highlight"><pre>&lt;<span class="c">#</span>
.SYNOPSIS
Fragt nach dem Teil eines Datums

.DESCRIPTION
Dient zum Erfassen der Bestandteile eines Datums. Macht keine Sicherheitsüberprüfungen

.PARAMETER Frage
Specifiziert die dem User zu stellende Frage.

.OUTPUTS
System.String. Gibt die von der Eingabeaufforderung übergebenen Zeichen zurück

.LINK
Read-Host
<span class="c">#&gt;</span>
<span class="k">function </span>get-DatumsTeil<span class="o">([</span>string<span class="o">]</span><span class="nv">$Frage</span><span class="o">)</span>
<span class="o">{</span>
        <span class="k">return </span>Read-Host -prompt <span class="nv">$Frage</span>
<span class="o">}</span>


<span class="k">function </span><span class="nb">set</span>-Monat<span class="o">()</span>
<span class="o">{</span>
        <span class="k">if</span><span class="o">(</span><span class="nv">$m</span> -le 2<span class="o">)</span>
        <span class="o">{</span>
           <span class="nv">$m</span> +<span class="o">=</span> 10;
                <span class="nv">$j</span> -<span class="o">=</span> 1;
        <span class="o">}</span>
        <span class="k">else</span>
        <span class="o">{</span>
           <span class="nv">$m</span> -<span class="o">=</span> 2;
        <span class="o">}</span>

        <span class="o">[</span>int<span class="o">]</span><span class="nv">$c</span> <span class="o">=</span> <span class="nv">$j</span>/100;
        <span class="o">[</span>int<span class="o">]</span><span class="nv">$y</span> <span class="o">=</span> <span class="nv">$j</span>%100;

        <span class="o">[</span>int<span class="o">]</span><span class="nv">$h</span> <span class="o">=</span> <span class="o">(((</span>26 * <span class="nv">$m</span> -2<span class="o">)</span>/10<span class="o">)</span> + <span class="nv">$t</span> + <span class="nv">$y</span> + <span class="nv">$y</span>/4 + <span class="nv">$c</span>/4 -2 * <span class="nv">$c</span><span class="o">)</span>%7

        <span class="k">if</span><span class="o">(</span><span class="nv">$h</span> -lt 0<span class="o">)</span>
        <span class="o">{</span>
                <span class="nv">$h</span> +<span class="o">=</span>7;
        <span class="o">}</span>

        <span class="k">return</span> <span class="nv">$h</span>
<span class="o">}</span>


<span class="k">function </span>get-Day<span class="o">(</span><span class="nv">$value</span><span class="o">)</span>
<span class="o">{</span>
   <span class="o">[</span>string<span class="o">]</span><span class="nv">$Tag</span> <span class="o">=</span> <span class="s2">&quot;&quot;</span>;
        switch<span class="o">(</span><span class="nv">$h</span><span class="o">)</span>
        <span class="o">{</span>
           0 <span class="o">{</span> <span class="nv">$Tag</span> <span class="o">=</span> <span class="s2">&quot;Sonntag&quot;</span>; <span class="nb">break</span><span class="o">}</span>

           1 <span class="o">{</span> <span class="nv">$Tag</span> <span class="o">=</span> <span class="s2">&quot;Montag&quot;</span>; <span class="nb">break</span><span class="o">}</span>

           2 <span class="o">{</span> <span class="nv">$Tag</span> <span class="o">=</span> <span class="s2">&quot;Dienstag&quot;</span>; <span class="nb">break</span><span class="o">}</span>

           3 <span class="o">{</span> <span class="nv">$Tag</span> <span class="o">=</span> <span class="s2">&quot;Mittwoch&quot;</span>; <span class="nb">break</span><span class="o">}</span>

           4 <span class="o">{</span> <span class="nv">$Tag</span> <span class="o">=</span> <span class="s2">&quot;Donnerstag&quot;</span>;<span class="nb">break</span><span class="o">}</span>

           5 <span class="o">{</span> <span class="nv">$Tag</span> <span class="o">=</span> <span class="s2">&quot;Freitag&quot;</span>; <span class="nb">break</span><span class="o">}</span>

           6 <span class="o">{</span> <span class="nv">$Tag</span> <span class="o">=</span> <span class="s2">&quot;Samstag&quot;</span>; <span class="nb">break</span><span class="o">}</span>
        <span class="o">}</span>

        <span class="k">return</span> <span class="nv">$Tag</span>
<span class="o">}</span>


<span class="nv">$t</span> <span class="o">=</span> get-DatumsTeil<span class="o">(</span><span class="s2">&quot;Geben Sie den Tag ein&quot;</span><span class="o">)</span>
<span class="nv">$m</span> <span class="o">=</span> get-DatumsTeil<span class="o">(</span><span class="s2">&quot;Geben Sie den  Monat ein&quot;</span><span class="o">)</span>
<span class="nv">$j</span> <span class="o">=</span> get-DatumsTeil<span class="o">(</span><span class="s2">&quot;Geben Sie das Jahr ein&quot;</span><span class="o">)</span>

<span class="nv">$MerkeMonat</span> <span class="o">=</span> <span class="nv">$m</span>
<span class="nv">$h</span> <span class="o">=</span> <span class="nb">set</span>-Monat
<span class="nv">$Tag</span> <span class="o">=</span> get-Day<span class="o">(</span><span class="nv">$h</span><span class="o">)</span>
Write-Host <span class="s2">&quot;Der &quot;</span> + <span class="nv">$t</span> +<span class="s2">&quot;.&quot;</span> + <span class="nv">$MerkeMonat</span> + <span class="s2">&quot;.&quot;</span> + <span class="nv">$j</span> + <span class="s2">&quot; ist ein &quot;</span> + <span class="nv">$Tag</span>
</pre></div>
</div>
</div></blockquote>
</li>
<li><p class="first">Schreiben Sie eine Funktion, die ihnen die Größe des vorhandenen physikalischen und virtuellen RAMs ausgibt. Benutzen Sie zum Ermitteln der Größe die WMI-Klasse WIN32_OperatingSystem und deren Eigenschaften <strong>FreePhysicalMemory</strong> sowie <strong>FreeVirtualMemory</strong>. Als kleine &#8220;Zugabe&#8221; möchten Sie die Angabe der Größe in verschiedenen Formaten (Byte, KByte, MByte) ermöglichen.</p>
</li>
</ul>
<div class="highlight-sh"><div class="highlight"><pre><span class="k">function </span>get-memory <span class="o">([</span>string<span class="o">]</span><span class="nv">$units</span> <span class="o">=</span> <span class="s2">&quot;b&quot;</span><span class="o">)</span> <span class="o">{</span>
         <span class="nv">$comp</span> <span class="o">=</span> get-wmiobject Win32_OperatingSystem
         <span class="nv">$mem</span> <span class="o">=</span> <span class="nv">$comp</span>.FreePhysicalMemory + <span class="nv">$comp</span>.FreeVirtualMemory
         switch <span class="o">(</span><span class="nv">$units</span><span class="o">)</span> <span class="o">{</span>
                  <span class="s2">&quot;b&quot;</span> <span class="o">{</span><span class="nv">$mem</span><span class="o">}</span>
                  <span class="s2">&quot;k&quot;</span> <span class="o">{[</span>int<span class="o">](</span><span class="nv">$mem</span> / 1024<span class="o">)}</span>
                  <span class="s2">&quot;m&quot;</span> <span class="o">{[</span>int<span class="o">]((</span><span class="nv">$mem</span> * 100<span class="o">)</span> / 1048576<span class="o">)</span> / 100<span class="o">}</span>
         <span class="o">}</span>
<span class="o">}</span>
</pre></div>
</div>
<ul class="simple">
<li>Schreiben Sie eine Funktion zum Umbenennen eines Computernamens. Der Name soll entweder per Eingabeaufforderung übergeben werden können oder aus einer Textdatei gelesen werden, falls keine Eingabe vorgenommen wird. Kommentieren Sie die Funktion über die Powershell-typische Hilfesyntax. Prüfen Sie innerhalb der Funktion, ob Sie überhaupt die administrativen Rechte für die Umbenennung haben. Zum Abschluss des Vorgangs müssen Sie einen Reboot vornehmen.</li>
</ul>
<p>siehe: <a class="reference external" href="http://www.gangleri.net/2009/11/25/ChangingComputerNameAndWorkgroupWithPowerShell.aspx">http://www.gangleri.net/2009/11/25/ChangingComputerNameAndWorkgroupWithPowerShell.aspx</a></p>
<div class="highlight-sh"><div class="highlight"><pre><span class="k">function </span>Set-ComputerName
<span class="o">{</span>
        &lt;<span class="c">#</span>
        .SYNOPSIS
                Sets the name of the computer
        .DESCRIPTION
                Uses WMI to <span class="nb">set </span>the name of the computer, <span class="k">if </span>the name is successfully changed the user will be prompted to reboot and apply the changes
        .NOTES
                File Name: ComputerName.psm1
                Aurthor: Alan Bradley
                Requires: PowerShell 2.0
        .LINK
                http://www.gangleri.net
        .EXAMPLE
                Set-ComputerName -Name <span class="s2">&quot;{New name for computer}&quot;</span>
        .EXAMPLE
                Set-ComputerName <span class="s2">&quot;{New name for computer}&quot;</span>
        .EXAMPLE
                Set-ComputerName -Name <span class="s2">&quot;{New name for computer}&quot;</span> -AutoReboot
        .EXAMPLE
                Set-ComputerName <span class="s2">&quot;{New name for computer}&quot;</span> -AutoReboot
        .PARAMETER Name
                The new name that the computer will be known by
        <span class="c">#&gt;</span>
        param<span class="o">(</span>
                <span class="o">[</span>Parameter<span class="o">(</span><span class="nv">Position</span><span class="o">=</span>0, <span class="nv">Mandatory</span><span class="o">=</span><span class="nv">$true</span>,ParameterSetName<span class="o">=</span><span class="s2">&quot;Name&quot;</span><span class="o">)]</span>
                <span class="o">[</span>string<span class="o">]</span><span class="nv">$Name</span>,
                <span class="o">[</span>Switch<span class="o">]</span><span class="nv">$AutoReboot</span>
        <span class="o">)</span>
        process
        <span class="o">{</span>
                <span class="nv">$sysInfo</span> <span class="o">=</span> Get-WmiObject -Class Win32_ComputerSystem
                <span class="nv">$result</span> <span class="o">=</span> <span class="nv">$sysInfo</span>.Rename<span class="o">(</span><span class="nv">$Name</span><span class="o">)</span>

                switch<span class="o">(</span><span class="nv">$result</span>.ReturnValue<span class="o">)</span>
                <span class="o">{</span>
                        0
                        <span class="o">{</span>
                                Write-Host -ForegroundColor Green <span class="s2">&quot;Success&quot;</span>
                                <span class="k">if</span><span class="o">(</span><span class="nv">$AutoReboot</span> -eq <span class="nv">$false</span><span class="o">)</span>
                                <span class="o">{</span>
                                        <span class="nv">$yes</span> <span class="o">=</span> New-Object System.Management.Automation.Host.ChoiceDescription <span class="s2">&quot;&amp;Yes&quot;</span>,
                                                <span class="s2">&quot;Reboots the machine.&quot;</span>
                                        <span class="nv">$no</span> <span class="o">=</span> New-Object System.Management.Automation.Host.ChoiceDescription <span class="s2">&quot;&amp;No&quot;</span>,
                                                <span class="s2">&quot;Does not reboot the machine, you will manually have to reboot this machine for changes to take effect.&quot;</span>
                                        <span class="nv">$options</span> <span class="o">=</span> <span class="o">[</span>System.Management.Automation.Host.ChoiceDescription<span class="o">[]](</span><span class="nv">$yes</span>, <span class="nv">$no</span><span class="o">)</span>
                                        <span class="nv">$ShouldReboot</span> <span class="o">=</span> <span class="nv">$Host</span>.UI.PromptForChoice<span class="o">(</span><span class="s2">&quot;Reboot machine&quot;</span>,
                                                <span class="s2">&quot;You must reboot for change to take effect. Do you want to reboot now?&quot;</span>, <span class="nv">$options</span>, 0<span class="o">)</span>
                                <span class="o">}</span>
                                <span class="k">if</span><span class="o">(</span><span class="nv">$ShouldReboot</span> -eq 0 -or <span class="nv">$AutoReboot</span> -eq <span class="nv">$true</span><span class="o">)</span>
                                <span class="o">{</span>
                                        Restart-Computer
                                <span class="o">}</span>
                        <span class="o">}</span>
                        5 <span class="o">{</span> Write-Host -ForegroundColor Red <span class="s2">&quot;This cmdlet must be execute with administrative privileges&quot;</span> <span class="o">}</span>
                        default <span class="o">{</span> Write-Host -ForegroundColor Red <span class="s2">&quot;Error&quot;</span> <span class="o">}</span>
                <span class="o">}</span>
        <span class="o">}</span>
<span class="o">}</span>
</pre></div>
</div>
<div class="highlight-sh"><div class="highlight"><pre>&lt;<span class="c">#</span>
.SYNOPSIS
        Renames a computer
.DESCRIPTION
        This Script will rename a computer by passing the oldComputername and the new Computername. If no names are given, a textfile will be openend
.EXAMPLE
        .<span class="se">\S</span>et-Computername.ps1 <span class="s1">&#39;oldName&#39;</span> <span class="s1">&#39;newName&#39;</span>
.PARAMETER originalPCName
        Old name of the computer
.PARAMETER computerName
        new name of the computer
.NOTES
        FileName     : Download-File.ps1
        Author       : Steinam
        LastModified : 06 Apr 2011 9:39 AM PST
<span class="c">#Requires -Version 2.0</span>
<span class="c">#&gt;</span>
<span class="k">function </span>Set-ComputerName <span class="o">{</span>
        param<span class="o">([</span>switch<span class="o">]</span><span class="nv">$help</span>,
                  <span class="o">[</span>string<span class="o">]</span><span class="nv">$originalPCName</span><span class="o">=</span><span class="k">$(</span><span class="nb">read</span>-host <span class="s2">&quot;Please specify the current name of the computer&quot;</span><span class="k">)</span>,
                  <span class="o">[</span>string<span class="o">]</span><span class="nv">$computerName</span><span class="o">=</span><span class="k">$(</span><span class="nb">read</span>-host <span class="s2">&quot;Please specify the new name of the computer&quot;</span><span class="k">)</span><span class="o">)</span>

        <span class="nv">$usage</span> <span class="o">=</span> <span class="s2">&quot;set-ComputerName -originalPCname CurrentName -computername AnewName&quot;</span>
        <span class="k">if</span> <span class="o">(</span><span class="nv">$help</span><span class="o">)</span> <span class="o">{</span>Write-Host <span class="nv">$usage</span>;<span class="nb">break</span><span class="o">}</span>

        <span class="nv">$computer</span> <span class="o">=</span> Get-WmiObject Win32_ComputerSystem -OriginalPCname OriginalName -computername <span class="nv">$originalPCName</span>
        <span class="nv">$computer</span>.Rename<span class="o">(</span><span class="nv">$computerName</span><span class="o">)</span>
        <span class="o">}</span>


        <span class="c">#set-Computername</span>
        <span class="nv">$computer</span> <span class="o">=</span> Get-WmiObject Win32_ComputerSystem
        <span class="nv">$computer</span> | Format-List -Property N*
</pre></div>
</div>
<ul>
<li><dl class="first docutils">
<dt>Powershell kann mit Hilfe des CommandLets <strong>Get-Eventlog</strong> die Ereignisanzeige von lokalen und remote-Computern anzeigen und filtern. Schreiben Sie eine Funktion <em>getLogInfos</em>, die Ihnen folgende Auswahlmöglichkeiten zulässt:</dt>
<dd><ul class="first simple">
<li>Wahl des Computers</li>
<li>Wahl des jeweiligen EventLogs</li>
<li>Wahl des jeweiligen Eintrags, z.B. Error, Warnig, Information</li>
</ul>
<p>Informieren Sie sich mit Hilfe von get-help über die weiteren Möglichkeiten von Get-Eventlog</p>
<div class="last highlight-sh"><div class="highlight"><pre><span class="k">function </span>getLogInfo<span class="o">([</span>string<span class="o">]</span><span class="nv">$Computer</span><span class="o">=</span><span class="s2">&quot;.&quot;</span>, <span class="o">[</span>string<span class="o">]</span><span class="nv">$EventLog</span>, <span class="o">[</span>string<span class="o">]</span><span class="nv">$ErrorType</span><span class="o">)</span>
<span class="o">{</span>


<span class="o">}</span>
</pre></div>
</div>
</dd>
</dl>
</li>
<li><p class="first">Verändern von Attributen einer Datei</p>
<p>Sie haben als Administrator einige Dateien, auf die sie von Zeit zu Zeit zugreifen müssen. Um die Dateien vor versehentlichen Änderungen zu schützen, versehen Sie diese mit ReadOnly - Flag. Leider ist aber jetzt ein Bearbeiten dieser Dateien mühsam, weil Sie nun immer erst dieses Flag ändern müssen.</p>
<p>Schreiben Sie ein parametrisiertes Skript, welches alle Dateien eines Ordners ReadOnly bzw. Nicht-ReadOnly setzen kann. Schreiben Sie zusätzlich eine Funktion, die Ihnen alle Dateien mit ReadOnly-Flag auflistet.
Eine Hilfe-Funktion soll Sie bei einer falschen Eingabe unterstützen.</p>
<div class="highlight-sh"><div class="highlight"><pre> http://blogs.technet.com/b/heyscriptingguy/archive/2009/09/24/hey-scripting-guy-september-24-2009.aspx

 Param <span class="o">([</span>string<span class="o">]</span><span class="nv">$path</span><span class="o">=</span><span class="s1">&#39;c:\fso&#39;</span>,
<span class="o">[</span>switch<span class="o">]</span><span class="nv">$get</span>,
<span class="o">[</span>switch<span class="o">]</span><span class="nv">$clear</span>,
<span class="o">[</span>switch<span class="o">]</span><span class="nv">$set</span>,
<span class="o">[</span>switch<span class="o">]</span><span class="nv">$help</span>
 <span class="o">)</span><span class="c">#end param</span>

 <span class="c"># *** Functions here ***</span>

 Function Get-ReadOnlyFiles<span class="o">([</span>string<span class="o">]</span><span class="nv">$path</span><span class="o">)</span>
 <span class="o">{</span>
  <span class="s2">&quot;Getting readonly attribute on files in $path&quot;</span>
  Get-ChildItem -Path <span class="nv">$path</span> |
  Where-Object <span class="o">{</span> <span class="nv">$_</span>.attributes -match <span class="s1">&#39;readonly&#39;</span> <span class="o">}</span>
 <span class="o">}</span> <span class="c">#end get-Readonlyfiles</span>

 Function Clear-ReadOnlyFiles<span class="o">([</span>string<span class="o">]</span><span class="nv">$path</span><span class="o">)</span>
 <span class="o">{</span>
   <span class="s2">&quot;Clearing readonly attribute from files in $path&quot;</span>
  New-Variable -Name read_only -Value 1 -Option <span class="nb">readonly</span>
<span class="nb">  </span>Get-ChildItem -Path <span class="nv">$path</span> |
  Where-Object <span class="o">{</span> <span class="nv">$_</span>.attributes -match <span class="s1">&#39;readonly&#39;</span> <span class="o">}</span> |
  ForEach-Object <span class="o">{</span>
    <span class="nv">$_</span>.attributes <span class="o">=</span> <span class="nv">$_</span>.attributes -Bxor <span class="nv">$read_only</span> <span class="o">}</span>
 <span class="o">}</span><span class="c">#end Clear-ReadonlyFiles</span>

 Function Set-ReadOnlyFiles<span class="o">([</span>string<span class="o">]</span><span class="nv">$path</span><span class="o">)</span>
 <span class="o">{</span>
  <span class="s2">&quot;Setting readonly attribute on files in $path&quot;</span>
  New-Variable -Name read_only -Value 1 -Option <span class="nb">readonly</span>
<span class="nb">  </span>Get-ChildItem -Path <span class="nv">$path</span> |
  ForEach-Object <span class="o">{</span>
    <span class="nv">$_</span>.IsReadOnly <span class="o">=</span> <span class="nv">$True</span>
  <span class="o">}</span>
 <span class="o">}</span><span class="c">#end Set-ReadOnlyFiles</span>

 Function Get-HelpText
 <span class="o">{</span>
  <span class="nv">$helpText</span> <span class="o">=</span> @<span class="s2">&quot;</span>
<span class="s2">   WorkWithReadOnlyFiles.ps1 -path c:\fso -get</span>
<span class="s2">   Gets a list of all readonly files in c:\fso folder</span>

<span class="s2">   WorkWithReadOnlyFiles.ps1 -path c:\fso -set</span>
<span class="s2">   Sets all files in the c:\fso folder to readonly</span>

<span class="s2">   WorkWithReadOnlyFiles.ps1 -path c:\fso -clear</span>
<span class="s2">   Clears the readonly attribute from all files in c:\fso folder</span>
<span class="s2"> &quot;</span>@
  <span class="nv">$helpText</span>
 <span class="o">}</span><span class="c">#end Get-HelpText</span>


 <span class="c"># *** Entry Point to Script ***</span>
 <span class="c">#$clear = $true</span>
 <span class="k">if</span><span class="o">(</span><span class="nv">$help</span><span class="o">)</span> <span class="o">{</span> Get-HelpText ; <span class="nb">exit</span> <span class="o">}</span>
 <span class="k">if</span><span class="o">(</span>!<span class="nv">$path</span><span class="o">)</span> <span class="o">{</span> <span class="s2">&quot;A path is required.&quot;</span> ; Get-HelpText ; <span class="nb">exit</span> <span class="o">}</span>
 <span class="k">if</span><span class="o">(</span><span class="nv">$get</span><span class="o">)</span> <span class="o">{</span> Get-ReadOnlyFiles -path <span class="nv">$path</span> ; <span class="nb">exit</span> <span class="o">}</span>
 <span class="k">if</span><span class="o">(</span><span class="nv">$clear</span><span class="o">)</span> <span class="o">{</span> Clear-ReadOnlyFiles -path <span class="nv">$path</span> ; <span class="nb">exit</span> <span class="o">}</span>
 <span class="k">if</span><span class="o">(</span><span class="nv">$set</span><span class="o">)</span> <span class="o">{</span> Set-ReadonlyFiles -path <span class="nv">$path</span> ; <span class="nb">exit</span> <span class="o">}</span>
</pre></div>
</div>
</li>
<li><p class="first">Ein Außendienstmitarbeiter erhält ein neues Firmen-Laptop. Auf seinem alten Laptop sind viele WLAN-Profile gespeichert und Sie suchen einen Weg, um diese Profile auf den neuen Rechner ohne manuelles Neuanlegen zu übernehmen. Ein Kollege erzählt Ihnen, dass Sie mit Hilfe des Befehls netsh Profile sowie exportieren als auch importieren können.</p>
<ul>
<li><p class="first">Informieren Sie sich über die Möglichkeiten des Befehls netsh</p>
</li>
<li><p class="first">Schreiben Sie ein Skript, welches sowohl den Export als auch den Import der WLAN-Profile ermöglicht.</p>
<blockquote>
<div><div class="highlight-sh"><pre>function Export-WLAN {
&lt;#
.SYNOPSIS
        Exports all-user WLAN profiles
.DESCRIPTION
        Exports all-user WLAN profiles to Xml-files to the specified directory using netsh.exe
.PARAMETER XmlDirectory
        Directory to export Xml configuration-files to
.EXAMPLE
        Export-WLAN -XmlDirectory c:\temp\wlan
#&gt;

[CmdletBinding()]
        param (
                [parameter(Mandatory=$true)]
                [string]$XmlDirectory
                )

#Export all WLAN profiles to specified directory
$wlans = netsh wlan show profiles | Select-String -Pattern "Profil für alle Benutzer" | Foreach-Object {$_.ToString()}
$exportdata = $wlans | Foreach-Object {$_.Replace("    Profil für alle Benutzer : ",$null)}
$exportdata | ForEach-Object {netsh wlan export profile $_ $XmlDirectory}
}

function Import-WLAN {
&lt;#
.SYNOPSIS
        Imports all-user WLAN profiles based on Xml-files in the specified directory
.DESCRIPTION
        Imports all-user WLAN profiles based on Xml-files in the specified directory using netsh.exe
.PARAMETER XmlDirectory
        Directory to import Xml configuration-files from
.EXAMPLE
        Import-WLAN -XmlDirectory c:\temp\wlan
#&gt;

[CmdletBinding()]
        param (
                [parameter(Mandatory=$true)]
                [string]$XmlDirectory
                )

#Import all WLAN Xml-files from specified directory
Get-ChildItem $XmlDirectory | Where-Object {$_.extension -eq ".xml"} | ForEach-Object {netsh wlan add profile filename=($XmlDirectory+"\"+$_.name)}
}</pre>
</div>
</div></blockquote>
</li>
</ul>
</li>
<li><p class="first">Aufruf von Remote-Desktop-Sitzungen</p>
<blockquote>
<div><p>Erstellen Sie ein Skript, welches eine rdp-Verbindung unter Berücksichtigung verschiedener Optionen des mstsc-Befehls ermöglicht.</p>
<div class="highlight-sh"><div class="highlight"><pre><span class="c">########################################################################################################################</span>
    <span class="c"># NAME</span>
    <span class="c">#     Start-RDP</span>
    <span class="c">#</span>
    <span class="c"># SYNOPSIS</span>
    <span class="c">#     Opens a remote desktop connection to another computer.</span>
    <span class="c">#</span>
    <span class="c"># SYNTAX</span>
    <span class="c">#     Start-RDP [[-Server] &lt;string&gt;] [[-Width] &lt;int&gt;] [[-Height] &lt;int&gt;]</span>
    <span class="c">#     Start-RDP -Path &lt;string&gt; [[-Width] &lt;int&gt;] [[-Height] &lt;int&gt;]</span>
    <span class="c">#</span>
    <span class="c"># DETAILED DESCRIPTION</span>
    <span class="c">#     The Start-RDP cmdlet opens a new Remote Desktop connection using the Microsoft Terminal Services Client.</span>
    <span class="c">#     Connection settings can be specified by argument or read from a standard RDP file.</span>
    <span class="c">#</span>
    <span class="c"># PARAMETERS</span>
    <span class="c">#     -Server &lt;string&gt;</span>
    <span class="c">#         Specifies the name of the server to connect to.  May also include an IP address, domain, and/or port.</span>
    <span class="c">#</span>
    <span class="c">#         Required?                    false</span>
    <span class="c">#         Position?                    1</span>
    <span class="c">#         Default value</span>
    <span class="c">#         Accept pipeline input?       true</span>
    <span class="c">#         Accept wildcard characters?  false</span>
    <span class="c">#</span>
    <span class="c">#     -Width &lt;int&gt;</span>
    <span class="c">#         Specifies the desired width of the resolution for the connection (for non-full screen connections).</span>
    <span class="c">#</span>
    <span class="c">#         Required?                    false</span>
    <span class="c">#         Position?                    2</span>
    <span class="c">#         Default value</span>
    <span class="c">#         Accept pipeline input?       false</span>
    <span class="c">#         Accept wildcard characters?  false</span>
    <span class="c">#</span>
    <span class="c">#     -Height &lt;int&gt;</span>
    <span class="c">#         Specifies the desired height of the resolution for the connection (for non-full screen connections).</span>
    <span class="c">#</span>
    <span class="c">#         Required?                    false</span>
    <span class="c">#         Position?                    3</span>
    <span class="c">#         Default value</span>
    <span class="c">#         Accept pipeline input?       false</span>
    <span class="c">#         Accept wildcard characters?  false</span>
    <span class="c">#</span>
    <span class="c">#     -Path &lt;string&gt;</span>
    <span class="c">#         Specifies the path to an RDP file to connect with (resolution settings can be overridden using the</span>
    <span class="c">#         -Width and -Height parameters.</span>
    <span class="c">#</span>
    <span class="c">#         Required?                    false</span>
    <span class="c">#         Position?                    4</span>
    <span class="c">#         Default value</span>
    <span class="c">#         Accept pipeline input?       true</span>
    <span class="c">#         Accept wildcard characters?  false</span>
    <span class="c">#</span>
    <span class="c">#     -Console &lt;SwitchParameter&gt;</span>
    <span class="c">#         Connect to a Windows Server 2003 console session.</span>
    <span class="c">#</span>
    <span class="c">#         Required?                    false</span>
    <span class="c">#         Position?                    named</span>
    <span class="c">#         Default value                false</span>
    <span class="c">#         Accept pipeline input?       false</span>
    <span class="c">#         Accept wildcard characters?  false</span>
    <span class="c">#</span>
    <span class="c">#     -Admin &lt;SwitchParameter&gt;</span>
    <span class="c">#         Connect to a Windows Server 2008 administrator session.</span>
    <span class="c">#</span>
    <span class="c">#         Required?                    false</span>
    <span class="c">#         Position?                    named</span>
    <span class="c">#         Default value                false</span>
    <span class="c">#         Accept pipeline input?       false</span>
    <span class="c">#         Accept wildcard characters?  false</span>
    <span class="c">#</span>
    <span class="c">#     -Fullscreen &lt;SwitchParameter&gt;</span>
    <span class="c">#         Open connection in full screen mode.</span>
    <span class="c">#</span>
    <span class="c">#         Required?                    false</span>
    <span class="c">#         Position?                    named</span>
    <span class="c">#         Default value                false</span>
    <span class="c">#         Accept pipeline input?       false</span>
    <span class="c">#         Accept wildcard characters?  false</span>
    <span class="c">#</span>
    <span class="c">#     -Public &lt;SwitchParameter&gt;</span>
    <span class="c">#         Run Remote Desktop in public mode.</span>
    <span class="c">#</span>
    <span class="c">#         Required?                    false</span>
    <span class="c">#         Position?                    named</span>
    <span class="c">#         Default value                false</span>
    <span class="c">#         Accept pipeline input?       false</span>
    <span class="c">#         Accept wildcard characters?  false</span>
    <span class="c">#</span>
    <span class="c">#     -Span &lt;SwitchParameter&gt;</span>
    <span class="c">#         Span the Remote Desktop connection across multiple monitors.  Each monitor must have the same height</span>
    <span class="c">#         and be arranged vertically.</span>
    <span class="c">#</span>
    <span class="c">#         Required?                    false</span>
    <span class="c">#         Position?                    named</span>
    <span class="c">#         Default value                false</span>
    <span class="c">#         Accept pipeline input?       false</span>
    <span class="c">#         Accept wildcard characters?  false</span>
    <span class="c">#</span>
    <span class="c"># INPUT TYPE</span>
    <span class="c">#     String,System.IO.FileInfo</span>
    <span class="c">#</span>
    <span class="c"># RETURN TYPE</span>
    <span class="c">#</span>
    <span class="c">#</span>
    <span class="c"># NOTES</span>
    <span class="c">#</span>
    <span class="c">#     -------------------------- EXAMPLE 1 --------------------------</span>
    <span class="c">#</span>
    <span class="c">#     C:\PS&gt;Start-RDP</span>
    <span class="c">#</span>
    <span class="c">#</span>
    <span class="c">#     This command opens the Terminal Services Client connection dialog to specify a connection.</span>
    <span class="c">#</span>
    <span class="c">#</span>
    <span class="c">#     -------------------------- EXAMPLE 2 --------------------------</span>
    <span class="c">#</span>
    <span class="c">#     C:\PS&gt;Start-RDP -Server myserver -Width 1024 -Height 768</span>
    <span class="c">#</span>
    <span class="c">#</span>
    <span class="c">#     This command opens a new Remote Desktop connection to the server named &quot;myserver&quot; in a window with 1024x768 resolution.</span>
    <span class="c">#</span>
    <span class="c">#</span>
    <span class="c">#     -------------------------- EXAMPLE 3 --------------------------</span>
    <span class="c">#</span>
    <span class="c">#     C:\PS&gt;Start-RDP -Server myserver -Fullscreen</span>
    <span class="c">#</span>
    <span class="c">#</span>
    <span class="c">#     This command opens a new full screen Remote Desktop connection to the server named &quot;myserver&quot;.</span>
    <span class="c">#</span>
    <span class="c">#</span>
    <span class="c">#     -------------------------- EXAMPLE 4 --------------------------</span>
    <span class="c">#</span>
    <span class="c">#     C:\PS&gt;Start-RDP -Path C:\myserver.rdp</span>
    <span class="c">#</span>
    <span class="c">#</span>
    <span class="c">#     This command opens a new Remote Desktop connection using the specified RDP file.</span>
    <span class="c">#</span>
    <span class="c">#</span>

    <span class="c">#Function global:Start-RDP {</span>
            param<span class="o">(</span>
                    <span class="o">[</span>string<span class="o">]</span><span class="nv">$Server</span> <span class="o">=</span> <span class="s2">&quot;&quot;</span>,
                    <span class="o">[</span>int<span class="o">]</span><span class="nv">$Width</span> <span class="o">=</span> <span class="s2">&quot;&quot;</span>,
                    <span class="o">[</span>int<span class="o">]</span><span class="nv">$Height</span> <span class="o">=</span> <span class="s2">&quot;&quot;</span>,
                    <span class="o">[</span>string<span class="o">]</span><span class="nv">$Path</span> <span class="o">=</span> <span class="s2">&quot;&quot;</span>,
                    <span class="o">[</span>switch<span class="o">]</span><span class="nv">$Console</span>,
                    <span class="o">[</span>switch<span class="o">]</span><span class="nv">$Admin</span>,
                    <span class="o">[</span>switch<span class="o">]</span><span class="nv">$Fullscreen</span>,
                    <span class="o">[</span>switch<span class="o">]</span><span class="nv">$Public</span>,
                    <span class="o">[</span>switch<span class="o">]</span><span class="nv">$Span</span>
            <span class="o">)</span>

            begin <span class="o">{</span>
                    <span class="nv">$arguments</span> <span class="o">=</span> <span class="s2">&quot;&quot;</span>
                    <span class="nv">$dimensions</span> <span class="o">=</span> <span class="s2">&quot;&quot;</span>
                    <span class="nv">$processed</span> <span class="o">=</span> <span class="nv">$false</span>

                    <span class="k">if</span> <span class="o">(</span><span class="nv">$admin</span><span class="o">)</span> <span class="o">{</span>
                            <span class="nv">$arguments</span> +<span class="o">=</span> <span class="s2">&quot;/admin &quot;</span>
                    <span class="o">}</span> elseif <span class="o">(</span><span class="nv">$console</span><span class="o">)</span> <span class="o">{</span>
                            <span class="nv">$arguments</span> +<span class="o">=</span> <span class="s2">&quot;/console &quot;</span>
                    <span class="o">}</span>
                    <span class="k">if</span> <span class="o">(</span><span class="nv">$fullscreen</span><span class="o">)</span> <span class="o">{</span>
                            <span class="nv">$arguments</span> +<span class="o">=</span> <span class="s2">&quot;/f &quot;</span>
                    <span class="o">}</span>
                    <span class="k">if</span> <span class="o">(</span><span class="nv">$public</span><span class="o">)</span> <span class="o">{</span>
                            <span class="nv">$arguments</span> +<span class="o">=</span> <span class="s2">&quot;/public &quot;</span>
                    <span class="o">}</span>
                    <span class="k">if</span> <span class="o">(</span><span class="nv">$span</span><span class="o">)</span> <span class="o">{</span>
                            <span class="nv">$arguments</span> +<span class="o">=</span> <span class="s2">&quot;/span &quot;</span>
                    <span class="o">}</span>

                    <span class="k">if</span> <span class="o">(</span><span class="nv">$width</span> -and <span class="nv">$height</span><span class="o">)</span> <span class="o">{</span>
                            <span class="nv">$dimensions</span> <span class="o">=</span> <span class="s2">&quot;/w:$width /h:$height&quot;</span>
                    <span class="o">}</span>
            <span class="o">}</span>

            process <span class="o">{</span>
                    Function script:executePath<span class="o">([</span>string<span class="o">]</span><span class="nv">$path</span><span class="o">)</span> <span class="o">{</span>
                            Invoke-Expression <span class="s2">&quot;mstsc.exe &#39;$path&#39; $dimensions $arguments&quot;</span>
                    <span class="o">}</span>
                    Function script:executeArguments<span class="o">([</span>string<span class="o">]</span><span class="nv">$Server</span><span class="o">)</span> <span class="o">{</span>
                            Invoke-Expression <span class="s2">&quot;mstsc.exe /v:$server $dimensions $arguments&quot;</span>
                    <span class="o">}</span>

                    <span class="k">if</span> <span class="o">(</span><span class="nv">$_</span><span class="o">)</span> <span class="o">{</span>
                            <span class="k">if</span> <span class="o">(</span><span class="nv">$_</span> -is <span class="o">[</span>string<span class="o">])</span> <span class="o">{</span>
                                    <span class="k">if</span> <span class="o">(</span><span class="nv">$_</span> -imatch <span class="s1">&#39;\.rdp$&#39;</span><span class="o">)</span> <span class="o">{</span>
                                            <span class="k">if</span> <span class="o">(</span>Test-Path <span class="nv">$_</span><span class="o">)</span> <span class="o">{</span>
                                                    executePath <span class="nv">$_</span>
                                                    <span class="nv">$processed</span> <span class="o">=</span> <span class="nv">$true</span>
                                            <span class="o">}</span> <span class="k">else</span> <span class="o">{</span>
                                                    throw <span class="s2">&quot;Path does not exist.&quot;</span>
                                            <span class="o">}</span>
                                    <span class="o">}</span> <span class="k">else</span> <span class="o">{</span>
                                            executeArguments <span class="nv">$_</span>
                                            <span class="nv">$processed</span> <span class="o">=</span> <span class="nv">$true</span>
                                    <span class="o">}</span>
                            <span class="o">}</span> elseif <span class="o">(</span><span class="nv">$_</span> -is <span class="o">[</span>System.IO.FileInfo<span class="o">])</span> <span class="o">{</span>
                                    <span class="k">if</span> <span class="o">(</span>Test-Path <span class="nv">$_</span>.FullName<span class="o">)</span> <span class="o">{</span>
                                            executePath <span class="nv">$_</span>.FullName
                                            <span class="nv">$processed</span> <span class="o">=</span> <span class="nv">$true</span>
                                    <span class="o">}</span> <span class="k">else</span> <span class="o">{</span>
                                            throw <span class="s2">&quot;Path does not exist.&quot;</span>
                                    <span class="o">}</span>
                            <span class="o">}</span> elseif <span class="o">(</span><span class="nv">$_</span>.Path<span class="o">)</span> <span class="o">{</span>
                                    <span class="k">if</span> <span class="o">(</span>Test-Path <span class="nv">$_</span>.Path<span class="o">)</span> <span class="o">{</span>
                                            executePath <span class="nv">$_</span>.Path
                                            <span class="nv">$processed</span> <span class="o">=</span> <span class="nv">$true</span>
                                    <span class="o">}</span> <span class="k">else</span> <span class="o">{</span>
                                            throw <span class="s2">&quot;Path does not exist.&quot;</span>
                                    <span class="o">}</span>
                            <span class="o">}</span> elseif <span class="o">(</span><span class="nv">$_</span>.DnsName<span class="o">)</span> <span class="o">{</span>
                                    executeArguments <span class="nv">$_</span>.DnsName
                                    <span class="nv">$processed</span> <span class="o">=</span> <span class="nv">$true</span>
                            <span class="o">}</span> elseif <span class="o">(</span><span class="nv">$_</span>.Server<span class="o">)</span> <span class="o">{</span>
                                    executeArguments <span class="nv">$_</span>.Server
                                    <span class="nv">$processed</span> <span class="o">=</span> <span class="nv">$true</span>
                            <span class="o">}</span> elseif <span class="o">(</span><span class="nv">$_</span>.ServerName<span class="o">)</span> <span class="o">{</span>
                                    executeArguments <span class="nv">$_</span>.ServerName
                                    <span class="nv">$processed</span> <span class="o">=</span> <span class="nv">$true</span>
                            <span class="o">}</span> elseif <span class="o">(</span><span class="nv">$_</span>.Name<span class="o">)</span> <span class="o">{</span>
                                    executeArguments <span class="nv">$_</span>.Name
                                    <span class="nv">$processed</span> <span class="o">=</span> <span class="nv">$true</span>
                            <span class="o">}</span>
                    <span class="o">}</span>
            <span class="o">}</span>

            end <span class="o">{</span>
                    <span class="k">if</span> <span class="o">(</span><span class="nv">$path</span><span class="o">)</span> <span class="o">{</span>
                            <span class="k">if</span> <span class="o">(</span>Test-Path <span class="nv">$path</span><span class="o">)</span> <span class="o">{</span>
                                    Invoke-Expression <span class="s2">&quot;mstsc.exe &#39;$path&#39; $dimensions $arguments&quot;</span>

                            <span class="o">}</span> <span class="k">else</span> <span class="o">{</span>
                                    throw <span class="s2">&quot;Path does not exist.&quot;</span>
                            <span class="o">}</span>
                    <span class="o">}</span> elseif <span class="o">(</span><span class="nv">$server</span><span class="o">)</span> <span class="o">{</span>
                            Invoke-Expression <span class="s2">&quot;mstsc.exe /v:$server $dimensions $arguments&quot;</span>
                    <span class="o">}</span> elseif <span class="o">(</span>-not <span class="nv">$processed</span><span class="o">)</span> <span class="o">{</span>
                            Invoke-Expression <span class="s2">&quot;mstsc.exe $dimensions $arguments&quot;</span>
                    <span class="o">}</span>
            <span class="o">}</span>
    <span class="c">#}</span>
</pre></div>
</div>
</div></blockquote>
</li>
<li><p class="first">Setzen Sie mit Hilfe der PS die IP-Adresse eines Rechners. Geben Sie auch die Daten für Gateway, SN-Mask, NIC, DNS-Server</p>
<blockquote>
<div><div class="highlight-sh"><div class="highlight"><pre>        <span class="c"># corrected version - $mask variable corrected to match in both places</span>
        <span class="k">function </span>Set-IPAddress <span class="o">{</span>
    param<span class="o">(</span>  <span class="o">[</span>string<span class="o">]</span><span class="nv">$networkinterface</span> <span class="o">=</span><span class="k">$(</span><span class="nb">read</span>-host <span class="s2">&quot;Enter the name</span>
<span class="s2">                    of the NIC (ie Local Area Connection)&quot;</span><span class="k">)</span>,
                    <span class="o">[</span>string<span class="o">]</span><span class="nv">$ip</span> <span class="o">=</span> <span class="k">$(</span><span class="nb">read</span>-host <span class="s2">&quot;Enter an IP Address (ie 10.10.10.10)&quot;</span><span class="k">)</span>,
                    <span class="o">[</span>string<span class="o">]</span><span class="nv">$mask</span> <span class="o">=</span> <span class="k">$(</span><span class="nb">read</span>-host <span class="s2">&quot;Enter the subnet mask (ie 255.255.255.0)&quot;</span><span class="k">)</span>,
                    <span class="o">[</span>string<span class="o">]</span><span class="nv">$gateway</span> <span class="o">=</span> <span class="k">$(</span><span class="nb">read</span>-host <span class="s2">&quot;Enter the current name of the NIC you want to rename&quot;</span><span class="k">)</span>,
                    <span class="o">[</span>string<span class="o">]</span><span class="nv">$dns1</span> <span class="o">=</span> <span class="k">$(</span><span class="nb">read</span>-host <span class="s2">&quot;Enter the first DNS Server (ie 10.2.0.28)&quot;</span><span class="k">)</span>,
                    <span class="o">[</span>string<span class="o">]</span><span class="nv">$dns2</span>,
                    <span class="o">[</span>string<span class="o">]</span><span class="nv">$registerDns</span> <span class="o">=</span> <span class="s2">&quot;TRUE&quot;</span>
                    <span class="o">)</span>

                    <span class="nv">$dns</span> <span class="o">=</span> <span class="nv">$dns1</span>
    <span class="k">if</span><span class="o">(</span><span class="nv">$dns2</span><span class="o">){</span><span class="nv">$dns</span> <span class="o">=</span><span class="s2">&quot;$dns1,$dns2&quot;</span><span class="o">}</span>
    <span class="nv">$index</span> <span class="o">=</span> <span class="o">(</span>gwmi Win32_NetworkAdapter | where <span class="o">{</span><span class="nv">$_</span>.netconnectionid -eq <span class="nv">$networkinterface</span><span class="o">})</span>.InterfaceIndex
    <span class="nv">$NetInterface</span> <span class="o">=</span> Get-WmiObject Win32_NetworkAdapterConfiguration | where <span class="o">{</span><span class="nv">$_</span>.InterfaceIndex -eq <span class="nv">$index</span><span class="o">}</span>
    <span class="nv">$NetInterface</span>.EnableStatic<span class="o">(</span><span class="nv">$ip</span>, <span class="nv">$mask</span><span class="o">)</span>
    <span class="nv">$NetInterface</span>.SetGateways<span class="o">(</span><span class="nv">$gateway</span><span class="o">)</span>
    <span class="nv">$NetInterface</span>.SetDNSServerSearchOrder<span class="o">(</span><span class="nv">$dns</span><span class="o">)</span>
    <span class="nv">$NetInterface</span>.SetDynamicDNSRegistration<span class="o">(</span><span class="nv">$registerDns</span><span class="o">)</span>

<span class="o">}</span>
</pre></div>
</div>
</div></blockquote>
</li>
<li><p class="first">Mit Hilfe des Befehls <strong>net view \UNC-Name</strong> können Sie die Freigaben eines Rechners ermitteln. Schreiben Sie eine Funktion, die als Parameter einen UNC-Namen und einen speziellen Freigabetyp erwartet. Die Funktion soll dann die gefundenen Freigaben zurückgeben.</p>
</li>
</ul>
<div class="highlight-sh"><div class="highlight"><pre><span class="k">function </span>Get-ShareNames<span class="o">([</span>string<span class="o">]</span><span class="nv">$server</span>, <span class="o">[</span>string<span class="o">]</span><span class="nv">$ShareType</span><span class="o">)</span>
<span class="o">{</span>
&lt;<span class="c">#</span>
        .SYNOPSIS
                Get-Sharenames

        .DESCRIPTION
                Gets all printers from specified server
                using intern the net view <span class="nb">command</span>

        .Parameter Server
                Specified Servername who holds the shares

        .Parameter ShareType
                Specify <span class="nb">type </span>of shares, e.g. <span class="s2">&quot;Drucker&quot;</span> or <span class="s2">&quot;Platte&quot;</span>


<span class="c">#&gt;</span>

        <span class="nv">$result</span> <span class="o">=</span> net view <span class="nv">$server</span> | Select-String <span class="nv">$ShareType</span>
        <span class="c">#$result = net view &quot;\\win-srv11&quot; | Select-String &quot;Drucker&quot;</span>

        <span class="nv">$ausgabe</span> <span class="o">=</span> @<span class="o">()</span>


        foreach<span class="o">(</span><span class="nv">$element</span> in <span class="nv">$result</span><span class="o">)</span>
        <span class="o">{</span>
                 <span class="c">#nächste Zeile ist notwendig, da $element kein string</span>
                 <span class="c">#sondern ein matchInfo-Objekt ist</span>
                 <span class="c">#Wahrscheinlich wegen dem Select-String-ComandLet</span>
                 <span class="nv">$zeile</span> <span class="o">=</span> <span class="nv">$element</span>.toString<span class="o">()</span>
                 <span class="nv">$name</span> <span class="o">=</span> <span class="nv">$zeile</span>.split<span class="o">(</span><span class="s2">&quot;Drucker&quot;</span><span class="o">)</span>
                 <span class="nv">$ergebnis</span> <span class="o">=</span> <span class="nv">$name</span><span class="o">[</span>0<span class="o">]</span>.TrimEnd<span class="o">()</span>
                 <span class="nv">$ausgabe</span> +<span class="o">=</span> <span class="nv">$ergebnis</span>
        <span class="o">}</span>


        <span class="k">return</span> <span class="nv">$ausgabe</span>

<span class="o">}</span>


Get-Sharenames <span class="s2">&quot;\\win-srv11&quot;</span> <span class="s2">&quot;Drucker&quot;</span>


Get-ShareNames
</pre></div>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="Allgemeiner Index"
             >Index</a></li>
        <li><a href="../index.html">PS4SYS</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
        &copy; Copyright 2010, STE.
      Mit <a href="http://sphinx-doc.org/">Sphinx</a> 1.2b1 erstellt.
    </div>
  </body>
</html>